package jeni2.VCD;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.AdapterView.AdapterContextMenuInfo;

public class ViewVCD extends ListActivity {
	private static final int ACTIVITY_EDIT = 1;
	private static final int DELETE_ID = Menu.FIRST;
	private static final int EDIT_ID = Menu.FIRST+1;
	private static final int CANCEL = Menu.FIRST+2;
	private buatdatabase mDbHelper;
	//private TextView field,record;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.viewformvcd);
		
		mDbHelper = new buatdatabase(this);
       mDbHelper.open();
		
        fillData();
        registerForContextMenu(getListView());
        
        
		//field = (TextView) findViewById(R.id.field);
		//record = (TextView) findViewById(R.id.record);
		//dbAdapter = new adaptertablevcd(this);
		//dbAdapter.open();
		//Cursor cursor = dbAdapter.fetchAllItems();
		//cursor.moveToFirst();
		///while (cursor.isAfterLast() == false) {
		//	field.append("kode\t\t\tjudul");
		//	record.append(cursor.getString(1)+"\t\t"+cursor.getString(2)+"\n");
		//	record.append("-----------------------\n");
		//	cursor.moveToNext();
		//}
		//record.setOnClickListener(new OnClickListener() {
		//	public void onClick(View arg0) {
		//		// TODO Auto-generated method stub
		//		Intent intent = new Intent(ViewVCD.this, viewallvcd.class);
		//		startActivity(intent);
		//}
		//});
	}
	private void fillData() {
		// TODO Auto-generated method stub
		Cursor addressCursor = mDbHelper.fetchAllAddress();
    	startManagingCursor(addressCursor);
    	
    	String[] from = new String[] {buatdatabase.KEY_judul};
    	
    	int[] to = new int[] {R.id.text1};
    	
    	SimpleCursorAdapter address = new SimpleCursorAdapter(this, R.layout.address_row, addressCursor, from, to);
    	setListAdapter(address);
	}
	 public void onCreateContextMenu (ContextMenu menu, View v, ContextMenuInfo menuInfo)
	    {
	    	menu.add(0, DELETE_ID, 0, "Delete Data");
	    	menu.add(0, EDIT_ID, 0, "Edit Data");
	    	menu.add(0, CANCEL, 0, "Cancel");
	    	super.onCreateContextMenu(menu, v, menuInfo);
	    }
	 public boolean onContextItemSelected (MenuItem item)
	    {
	    	switch(item.getItemId())
	    	{
	    	case DELETE_ID:
	    		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
	    		mDbHelper.deleteAddress(info.id);
	    		fillData();
	    		return true;
	    	case EDIT_ID:
	    		AdapterContextMenuInfo info1 = (AdapterContextMenuInfo) item.getMenuInfo();
	    		Intent i = new Intent(this, viewallvcd.class);
	        	i.putExtra(buatdatabase.KEY_ROWID, info1.id);
	        	startActivityForResult(i, ACTIVITY_EDIT);
	    		return true;
	    	case CANCEL:
	    		return true;
	    	}
	    	return super.onContextItemSelected(item);
	    }
	    
	    @Override
	    protected void onListItemClick(ListView l, View v, int position, long id)
	    {
	    	super.onListItemClick(l, v, position, id); 
	    	Intent i = new Intent(this, viewallvcd.class);
	    	i.putExtra(buatdatabase.KEY_ROWID, id);
	    	startActivityForResult(i, ACTIVITY_EDIT);
	    }
	    
	    @Override
	    protected void onActivityResult(int requestCode, int resultCode, Intent intent)
	    {
	    	super.onActivityResult(requestCode, resultCode, intent);
	    	fillData();
	    }
}
